// releasePage/pages/announcePublish/announceList/announceList.js
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        ipxBottom: 0,
        textareaValue: '',
        editList: false,
        realname: '',
        recordList: [], //历史纪录
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        this.setData({
            editList: options.editList == '01' ? true : false,
            id: options.id,
            realname: app.globalData.realname,
        });
        this.getProjectNoticeRecord();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    onInput(e) {
        this.setData({
            textareaValue: e.detail.value,
        });
    },
    clearTextarea(e) {
        this.setData({
            textareaValue: '',
        });
    },
    // 提交
    submit(e) {
        let _that = this;
        wx.showModal({
            title: '确认提交？',
            content: '',
            success: function (res) {
                if (res.confirm) {
                    _that.createProjectNotice();
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 2, // 返回上一级页面。
                        });
                    }, 2000);
                } else {
                    console.log('点击取消回调');
                }
            },
        });
    },
    createProjectNotice() {
        let _that = this;
        let pages = getCurrentPages(); //当前页面栈
        let prevPage1 = pages[pages.length - 2];
        let prevPage2 = pages[pages.length - 3];
        let prevPage3 = pages[pages.length - 4];
        console.log(prevPage1.data);
        if (app.visitorPublish()) return; //游客发布限制
        let mustread = '00';
        if (prevPage1.data.importChoice[0].checked) {
            mustread = '01';
        }
        let data = {
            delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
            pjteamid: prevPage1.data.pjteamid, //项目组id
            coverpicurl: prevPage1.data.src, //封面图片
            text: prevPage1.data.htmlContent, //正文
            title: prevPage1.data.titleValue,
            mustread: mustread,
            nRecordText: _that.data.textareaValue,
        };
        //通告编辑
        if (prevPage1.data.noticeId) {
            data.id = prevPage1.data.noticeId;
            http.postRequest(
                app.globalData.rootPath +
                    `cjMap/editProjectNotice?authId=${app.globalData.authId}`,
                data
            ).then((res) => {
                if (res.status == 0) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000,
                    });
                    prevPage2.refresh();
                    if (prevPage3) {
                        prevPage3.refresh();
                    }
                }
            });
        } else {
            //通告发布
            http.postRequest(
                app.globalData.rootPath +
                    `cjMap/createProjectNotice?authId=${app.globalData.authId}`,
                data
            ).then((res) => {
                if (res.status == 0) {
                    prevPage2.refresh();
                    if (prevPage3) {
                        prevPage3.refresh();
                    }
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000,
                    });
                }
            });
        }
    },
    //获取通告操作记录
    getProjectNoticeRecord() {
        let _that = this;
        http.getRequest(
            `cjMap/getProjectNoticeRecord?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res);
                for (let i = 0; i < res.data.length; i++) {
                    res.data[i].createtime = app.timeago(
                        res.data[i].createtime
                    );
                }
                _that.setData({
                    recordList: res.data,
                });
            }
        });
    },
});
